import * as echarts from 'echarts/core';
// 导入所需的图表类型
import { BarChart, LineChart, PieChart, RadarChart, PictorialBarChart } from 'echarts/charts';
// 导入渲染器
import { SVGRenderer } from 'echarts/renderers';
// 导入组件
import { TooltipComponent, GridComponent, LegendComponent } from 'echarts/components';

// 注册所需的模块
echarts.use([
    BarChart,
    LineChart,
    PieChart,
    RadarChart,
    PictorialBarChart,
    SVGRenderer,
    TooltipComponent,
    GridComponent,
    LegendComponent,
]);

/**
 * 获取或创建 ECharts 实例
 * @param {HTMLElement} dom - DOM 元素
 * @param {string} [theme] - 主题名称
 * @returns {echarts.ECharts} ECharts 实例
 */
export default function useEcharts(dom, theme) {
    if (!dom) {
        console.warn('DOM 元素不可用');
        return null;
    }

    let instance = echarts.getInstanceByDom(dom);
    if (!instance) {
        instance = echarts.init(dom, theme, { renderer: 'svg' });
    }
    return instance;
}
